/*
 * @文件相对于项目的路径: /htxincailiao-webapp/src/store/modules/industry-exhibition.js
 * @message:
 */
import {
  getExhibitionListRequest,
  getExhibitionInfoRequest,
  exhibitionApplyRequest,
  getExhibitionApplyInfoRequest,
  getPCHomeListRequest,
} from '@/api/industry-exhibition'

const industryExhibition = {
  namespaced: true,
  state: {
    items: [],
    info: {},
    applyInfo: undefined,
    paymentId: undefined,
    pageSize: 20,
    currentPage: 1,
    originalPage: 1,
    pageTotal: 0,
    isHasMore: true,
  },

  mutations: {
    SET_ITEMS: (state, items) => {
      state.items = items
    },

    SET_INFO: (state, info) => {
      state.info = info
    },

    SET_APPLY_INFO: (state, applyInfo) => {
      state.applyInfo = applyInfo
    },

    SET_PAYMENT_ID: (state, paymentId) => {
      state.paymentId = paymentId
    },
    SET_CURRENT_PAGE: (state, currentPage) => {
      if (!currentPage) {
        currentPage = 1
      }
      state.currentPage = currentPage
    },

    SET_ORIGINAL_PAGE: (state, originalPage) => {
      if (!originalPage) {
        originalPage = 1
      }
      state.originalPage = originalPage
    },

    SET_PAGE_TOTAL: (state, pageTotal) => {
      state.pageTotal = pageTotal
    },

    SET_IS_HAS_MORE: (state, isHasMore) => {
      state.isHasMore = isHasMore
    },
  },

  actions: {
    getExhibitionListAction({ state, commit }, { isRefresh, parameters }) {
      if (isRefresh) {
        commit('SET_IS_HAS_MORE', false)
        commit('SET_CURRENT_PAGE', 1)
      }
      parameters = {
        ...parameters,
        pageNo: state.currentPage,
        pageSize: state.pageSize,
      }
      return new Promise((resolve, reject) => {
        getExhibitionListRequest(parameters)
          .then((response) => {
            if (response && response.result && response.result.records) {
              if (isRefresh) {
                commit('SET_ITEMS', [])
              }
              const items = response.result.records
              const pages = response.result.pages
              var current = state.currentPage
              if (current <= pages) {
                const concatedItems = state.items.concat(items)
                commit('SET_PAGE_TOTAL', pages)
                commit('SET_ITEMS', concatedItems)

                current += 1
                commit('SET_CURRENT_PAGE', current)
              }
              commit('SET_IS_HAS_MORE', current <= pages)

              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    getPCHomeExhibitionListAction({ state, commit }, { isRefresh, parameters }) {
      if (isRefresh) {
        commit('SET_IS_HAS_MORE', false)
        commit('SET_CURRENT_PAGE', 1)
      }
      parameters = {
        ...parameters,
        pageNo: state.currentPage,
        pageSize: state.pageSize,
      }
      return new Promise((resolve, reject) => {
        getPCHomeListRequest(parameters)
          .then((response) => {
            if (response && response.result && response.result.records) {
              if (isRefresh) {
                commit('SET_ITEMS', [])
              }
              const items = response.result.records
              const pages = response.result.pages
              var current = state.currentPage
              if (current <= pages) {
                const concatedItems = state.items.concat(items)
                commit('SET_PAGE_TOTAL', pages)
                commit('SET_ITEMS', concatedItems)

                current += 1
                commit('SET_CURRENT_PAGE', current)
              }
              commit('SET_IS_HAS_MORE', current <= pages)

              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    getExhibitionInfoAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        getExhibitionInfoRequest(parameters)
          .then((response) => {
            if (response && response.success) {
              if (response.result) {
                const data = response.result
                commit('SET_INFO', data)
              } else {
                commit('SET_INFO', null)
              }

              resolve()
            } else {
              reject(response)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    exhibitionApplyAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        exhibitionApplyRequest(parameters)
          .then((response) => {
            if (response && response.success) {
              const result = response.result
              if (result && result.paymentInfo && result.paymentInfo.id) {
                const paymentId = result.paymentInfo.id
                commit('SET_PAYMENT_ID', paymentId)
              }
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    getExhibitionApplyInfoAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        getExhibitionApplyInfoRequest(parameters)
          .then((response) => {
            if (response && response.success) {
              if (response.result) {
                const result = response.result
                commit('SET_APPLY_INFO', result)
              } else {
                commit('SET_APPLY_INFO', null)
              }

              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    updateCurrentPageAction({ commit }, payload) {
      commit('SET_CURRENT_PAGE', payload)
    },

    updateOriginalPageAction({ commit }, payload) {
      commit('SET_ORIGINAL_PAGE', payload)
    },
  },
}

export default industryExhibition

